package com.oldroid.apps.getit.ui;

import com.oldroid.apps.getit.R;
import com.oldroid.apps.getit.preferences.EmailPreference;
import com.oldroid.apps.getit.util.ActivityHelper;
import com.oldroid.apps.getit.util.UIUtils;

import android.content.Intent;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager.NameNotFoundException;
import android.os.Bundle;
import android.preference.PreferenceActivity;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;

public class SettingsActivity extends PreferenceActivity {
	final ActivityHelper mActivityHelper = ActivityHelper.createInstance(this);

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		mActivityHelper.setupActionBar(getTitle(), 0);
		addPreferencesFromResource(R.xml.preferences);
		if (UIUtils.isHoneycomb()) {
			this.getActionBar().setTitle("Settings");
		}

		// Find the app version number.
		String version;

		try {
			PackageInfo manager = getPackageManager().getPackageInfo(getPackageName(), 0);
			version = getString(R.string.app_name) + " " + manager.versionName;
		} catch (NameNotFoundException e) {
			version = "";
			Log.e("SettingsActivity", "The Package name is missing");
		}

		final EmailPreference emailPref = (EmailPreference) findPreference(getString(R.string.pref_feedback_key));
		emailPref.setVersion(version);
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		switch (item.getItemId()) {
		case android.R.id.home:
			goHome();
			return true;
		default:
			return super.onOptionsItemSelected(item);
		}
	}

	private void goHome() {
		final Intent intent = new Intent(this, HomeActivity.class);
		intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
		this.startActivity(intent);
		this.overridePendingTransition(R.anim.home_enter, R.anim.home_exit);
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// TODO Auto-generated method stub
		return super.onCreateOptionsMenu(menu);
	}

}
